<div class="tooltip-contain"></div>
<script>
(function(){
	var $tooltip = $(".tooltip");
	var $contain = $(".tooltip-contain");
	$tooltip.css({
		"background-color": "#fff",
		"border": "1px solid #CCCCCC",
		"padding": "5px 10px", 
		"width": "200px", 
		"color": "#E69700",
		"font-weight": "bold",
		"text-align": "left", 
		"font-size": "12px",
		"z-index": "1000",
		"position": "absolute",
		"margin": "5px",
		"display": "none",
	
		"-moz-border-radius-bottomleft": "3px",
		"-moz-border-radius-bottomright": "3px",
		"-moz-border-radius-topleft": "3px",
		"-moz-border-radius-topright": "3px"
	});

	$tooltip.each(function(){
		var $active = $(this).prev();
		var $tip = $(this).appendTo($contain);
		var $width = $(document).width();
		var $height = $(document).height();

		var m0 = function(e){
			if(e == undefined)return false;
			else if(e.pageX >= $tip.offset().left && e.pageX <= $tip.offset().left + $tip.outerWidth() && e.pageY >= $tip.offset().top && e.pageY <= $tip.offset().top + $tip.outerHeight()) return true;
			return false;
		};
		var m1 = function(e){
			$tip.css("left", $active.offset().left + ($active.outerWidth()- $tip.outerWidth(true))/2).css("top", $active.offset().top - $tip.outerHeight(true));
			if($tip.offset().left > $width - $tip.outerWidth(true)) $tip.css("left", $width - $tip.outerWidth(true));
			if($tip.offset().left < 0)$tip.css("left", 0);
			if($tip.offset().top < 0) return false;
			if(m0(e)) return false;
			return true;
		};
		var m2 = function(e){
			$tip.css("left", $active.offset().left + ($active.outerWidth()- $tip.outerWidth(true))/2).css("top", $active.offset().top + $active.outerHeight());
			if($tip.offset().left > $width - $tip.outerWidth(true)) $tip.css("left", $width - $tip.outerWidth(true));
			if($tip.offset().left < 0)$tip.css("left", 0);
			if($tip.offset().top > $height - $tip.outerHeight(true)) return false;
			if(m0(e)) return false;
			return true;
		};
		var m3 = function(e){
			$tip.css("left", $active.offset().left + $active.outerWidth(true)).css("top", $active.offset().top + ($active.outerHeight() - $tip.outerHeight(true))/2);
			if($tip.offset().top > $height - $tip.outerHeight(true)) $tip.css("top", $height - $tip.outerHeight(true));
			if($tip.offset().top < 0)$tip.css("top", 0);
			if($tip.offset().left > $width - $tip.outerWidth(true)) return false;
			if(m0(e)) return false;
			return true;
		};
		var m4 = function(e){
			$tip.css("left", $active.offset().left - $tip.outerWidth(true)).css("top", $active.offset().top + ($active.outerHeight() - $tip.outerHeight(true))/2);
			if($tip.offset().top > $height - $tip.outerHeight(true)) $tip.css("top", $height - $tip.outerHeight(true));
			if($tip.offset().top < 0)$tip.css("top", 0);
			if($tip.offset().left < 0) return false;
			if(m0(e)) return false;
			return true;
		};

		var open = function(e){
			$tip.hide();
			$width = $(document).width();
			$height = $(document).height();
			$tip.show();
			m1(e)?true:m2(e)?true:m3(e)?true:m4(e);
		};
		
		$tip.mousemove(function(e){
			open(e);
		});

		var $input = $active.filter("input");
		if($input.length){
			$input.focus(function(){
				open();
			}).blur(function(){
				$tip.hide();
			});
		};

		var $other = $active.not("input");
		if($other.length){
			$other.mouseenter(function(){
				open();
			}).mouseleave(function(){
				$tip.hide();
			});
		};
	});
})();
</script>